// pages/report/report.js
var dateTimePicker = require('../../utils/dateTimePicker.js');

Page({
  data: {
    mytime: true,
    report: false,
    startYear: 2000,
    endYear: 2050,
    index: 0,
    array: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
    num: 1,
    msgList: [],

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj.dateTimeArray.pop();
    var lastTime = obj.dateTime.pop();

    this.setData({
      dateTimeArray: obj.dateTimeArray,
      dateTime: obj.dateTime
    });
  },
  changeDateTime(e) {
    this.setData({
      dateTime: e.detail.value,
      mytime: false,
      report: true
    });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime,
      dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime,
      dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  numChange: function(e) {
    this.setData({
      index: e.detail.value,
      num: parseInt(e.detail.value) + 1
    })
  },
  clickNum: function(e) {
    var _id = e.currentTarget.dataset.id;
    var _num = e.currentTarget.dataset.num;
    this.setData({
      _id: _id,
      _num: _num
    })
  },
  // 添加客户信息
  insert: function() {
    var msgList = this.data.msgList;
    var val = this.data.num;
    var list = [];
    if (val >= 2 && val <= 9) {
      var add_num = parseInt(val) - msgList.length - 1;
      for (var i = 0; i < add_num; i++) {
        var item = {
          'is_male': null, //false/true
          'name': '',
          't_val': '',
          's_val': ''
        };
        msgList.push(item);
      }
      this.setData({
        msgList: msgList
      });
    }
  },
  // 获取姓名
  getName:function(e){
    var _id = e.currentTarget.dataset.id;
    var name = e.detail.value;
    var msgList = this.data.msgList;
    if (_id - 1 >= 0 && msgList[_id - 1]) {
      msgList[_id - 1].name = name;
      this.setData({
        msgList: msgList
      })
    }
  },
  // 获取号码前三位
  getTval: function (e) {
    var _id = e.currentTarget.dataset.id;
    var name = e.detail.value;
    var msgList = this.data.msgList;
    if (_id - 1 >= 0 && msgList[_id - 1]) {
      msgList[_id - 1].t_val = name;
      this.setData({
        msgList: msgList
      })
    }
  },
  // 获取号码前三位
  getSval: function (e) {
    var _id = e.currentTarget.dataset.id;
    var name = e.detail.value;
    var msgList = this.data.msgList;
    if (_id - 1 >= 0 && msgList[_id - 1]) {
      msgList[_id - 1].s_val = name;
      this.setData({
        msgList: msgList
      })
    }
  },
  delList: function(e) {
    let _index = e.currentTarget.dataset.index;
    let msgList = this.data.msgList;
    msgList.splice(parseInt(_index) - 1, 1);
    this.setData({
      msgList: msgList,
      index: this.data.msgList.length,
      num: parseInt(this.data.msgList.length) + 1
    });
  },
  clickSex: function(e) {
    var _id = e.currentTarget.dataset.id;
    var _num = e.currentTarget.dataset.num;
    var msgList = this.data.msgList;
    if (_id - 1 >= 0 && msgList[_id - 1]) {
      msgList[_id - 1].is_male = _num === '2' ? false : true;
      this.setData({
        msgList: msgList
      })
    }
  },
  // 点击提交
  showM: function() {
    wx.showModal({
      title: '提交成功',
      content: '请到猫眼看房APP查看处理结果',
      showCancel: true, //是否显示取消按钮
      cancelText: "我知道了", //默认是“取消”
      cancelColor: '#404040', //取消文字的颜色
      confirmText: "下载APP", //默认是“确定”
      confirmColor: '#ff5000', //确定文字的颜色
      success: function(res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定

        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})